<?php
/**
 * @author 刘国君
 * @version 1.0
 */
use Workerman\Connection\AsyncUdpConnection;
use app\Utils\File;
require_once dirname(__DIR__) . '/vendor/autoload.php';

$worker=new \Workerman\Worker('\\app\\Protocols\\Sip://0.0.0.0:9301');
//$worker = new \Workerman\Worker('udp://0.0.0.0:9301');
$worker->transport = 'udp';
$connections = [];
global $connections;

$_A=[];
$worker->onMessage=function(\Workerman\Connection\UdpConnection $connection,\app\Handel\SipMessage $message){

//    if($message->content){
//        var_dump($message->content);
////        $xml=simplexml_load_string($message->content);
//
////        $cmd_type=$xml->CmdType;
////        if($cmd_type!='Keepalive'){
////            var_dump($message->content);
////        }
//    }

    global $connections;
    $response=clone $message;
    $method=strtolower($message->method);
    if($connection->getRemotePort() == '9300'){
        $_A['qiuji'] = $connection;
        $_SESSION['qiuji'] = $connection;
        $connections['one']=$connection;
    }
    if($method=='register'){
        if(!isset($message->auth)){
            $response->code=401;
            $response->src_ip='192.168.1.80';
            $response->src_port=9301;

            $response->auth=[
                'realm'=>'3402000000',
                'nonce'=>'324cab6e557268e0'
            ];
        }else{
            $_SESSION['qiuji'] = $connection;
            $response->code=200;
            $connections['one']=$connection;
        }
        $connection->send($response->encode());
    }
    elseif ($method=='message') {
//        if ($message->xml_body['CmdType'][0] == 'Keepalive') {
            if($connection->getRemotePort() == '9300'){
                $response->code=200;
                $connection->send($response->encode());
            }
//        }
//        elseif ($message->xml_body['CmdType'][0]=='DeviceControl'){
        if ($connection->getRemotePort()!= '9300') {
                $response->code=200;
                $len = $connection->send($response->encode());
                if($len){
                    $response->code=0;
                    $response->cseq += 1;
                    $_SESSION['qiuji']->send($response->encode());
                }
        }
//    }
//        }
    }
    elseif ($method=='invite'){
        if ($connection->getRemotePort() != '9300') {
            var_dump("hello invite");
                $res = $_SESSION['qiuji']->send($response->encode());
        }
    }
    elseif ($method=='ack'){
        if ($connection->getRemotePort() != '9300') {
            var_dump('hello ack');
            $_SESSION['qiuji']->send($response->encode());
        }
    }
    elseif ($method=='bye'){
        if ($connection->getRemotePort() != '9300') {
            $res = $_SESSION['qiuji']->send($response->encode());
            sleep(10);
            File::file_move();
        }
    }
};

$worker->onWorkerStart = function (\Workerman\Worker $worker) {
    $inner = new \Workerman\Worker('udp://0.0.0.0:9302');
    $inner->onMessage = function (\Workerman\Connection\UdpConnection $connection, $message) {
//        global $connections;
//        $sip = $connections['one'];
//        $sip->send($message);
        if($message=='hi'){
            $connection->send("server hi");
        }
        if(strlen($message)>20){
            var_dump($message);
            $udp_connection = new AsyncUdpConnection('udp://127.0.0.1:9301');
            $udp_connection->onConnect = function($udp_connection) use($message){
                $udp_connection->send($message);
            };
            $udp_connection->connect();
        }
    };
    $inner->listen();
};

if (!defined('GLOBAL_START')) {
    \Workerman\Worker::runAll();
}